<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------

namespace app\model;

use \think\Model;

class Goods extends Model
{
    protected $name = 'goods';
    protected $autoWriteTimestamp = true;
    protected $insert = [];
    protected $update = [];

    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getShareCommissionAfterAttr($value, $data)
    {
        $price = $data['share_commission'] + $data['subsidy_commission'];
        return $price > 0 ? number_format($price / 100, 2) : 0;
    }

    public function category()
    {
        return $this->belongsTo(GoodsCategory::class, 'category_id')
            ->bind([
                'category_name' => 'name',
            ]);
    }

    public function type()
    {
        return $this->belongsTo(GoodsType::class, 'type')
            ->bind([
                'type_name' => 'name',
            ]);
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_appid', 'shop_appid')
            ->bind([
                'shop_name'  => 'name',
                'shop_image' => 'image',
            ]);
    }
}
